﻿using BLL;
using EncryptHelper;
using MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Script.Serialization;

namespace Loginter.ajax
{
    /// <summary>
    /// UserInfoUpdate 的摘要说明
    /// </summary>
    public class UserInfoUpdate : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //姓名
            string name = context.Request.Form["Name"];
            //性别
            string sex = context.Request.Form["Sex"];
            //角色id
            string role = context.Request.Form["Role"];
            //电话
            string phone = context.Request.Form["Phone"];
            //邮箱
            string email = context.Request.Form["Email"];
            //账户
            string account = context.Request.Form["Account"];
            //修改时间
            string time = DateTime.Now.ToString();
            //用户ID
            //int id =context.ToString["userid"];
            //电话进行格式和非空等数据验证
            if (!Regex.IsMatch(phone, @"^1[3578]\d{9}$"))
            {
                context.Response.Write("输入的电话格式不正确！");
                return;
            }
            //邮箱
            if (!Regex.IsMatch(email, @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$"))
            {
                context.Response.Write("输入的电子邮箱格式不正确！");
                return;
            }
            UserBLL man = new UserBLL();
            UpdateUserInfoModel model = new UpdateUserInfoModel()
            {
                //id
               // UserID = id,
                //姓名
                UserName = name,
                //性别
                Sex =sex,
                //账号
                Account = account,
                //角色id
                FK_RoleID = Convert.ToInt32(role),
                //电话
                Phone = phone,
                //邮箱
                Email = email,
                //修改时间
                AlterTime = Convert.ToDateTime(time)
            };
            int result = man.Update(model);
            JavaScriptSerializer js = new JavaScriptSerializer();
            if (result > 0)
            {
                context.Session["userid"] = model.UserID;
                OperateResult rsult = new OperateResult(1,"修改成功");
                string json = js.Serialize(rsult);
                context.Response.Write(json);
            }
            else
            {
                OperateResult orest = new OperateResult(-1, "修改失败！");
                string json = js.Serialize(orest);
               context.Response.Write(json);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}